package models

import (
	"GoTime/utils/conf"
	"fmt"

	// mysql
	_ "github.com/go-sql-driver/mysql"
	"xorm.io/xorm"
	"xorm.io/xorm/names"
)

var engine *xorm.Engine
var Engine *xorm.Engine

func init() {
	var err error
	dsn := CreateEngineSourceName()
	engine, err = xorm.NewEngine("mysql", dsn)
	if err != nil {
		fmt.Println(err.Error())
		panic(err.Error())
	}

	Engine = engine
	tbMapper := names.NewPrefixMapper(names.SnakeMapper{}, "dts_")
	engine.SetTableMapper(tbMapper)
	engine.ShowSQL(true)
}

// CreateEngineSourceName create engine source name
func CreateEngineSourceName() string {
	Host := conf.GetString("mysql.host")
	Port := conf.GetString("mysql.port")
	User := conf.GetString("mysql.user")
	Pass := conf.GetString("mysql.pass")
	Name := conf.GetString("mysql.name")
	//TablePrefix := conf.GetString("mysql.master.prefix")
	return User + ":" + Pass + "@tcp(" + Host + ":" + Port + ")/" + Name + "?charset=utf8"
}
